package edu.spsu.cs4253;


import java.net.*;
import java.io.*;

public class UDPServer {

	/**
	 * UDPServer will receive request and then send data to client
	 * 
	 */
	public static void main(String[] args) throws Exception {
		  // The chosen port of the server is: 69 //
		ServerSocket serverSocket = new ServerSocket(8089);
		String clientMessage;
		String modifiedMessage;
		
		while(true) {
			Socket connectionSocket = serverSocket.accept();
			BufferedReader fromClient = new BufferedReader (new InputStreamReader(connectionSocket.getInputStream()));
			DataOutputStream toClient = new DataOutputStream (connectionSocket.getOutputStream());
			clientMessage = fromClient.readLine();
			System.out.println("RECEIVED" + clientMessage);
			modifiedMessage = clientMessage.toUpperCase() + '\n';
			toClient.writeBytes(modifiedMessage);
			
			//Not quite sure if this is needed
			connectionSocket.close();
		}
		
		
		
		
		
		
		
		/*UDP
			DatagramSocket serverSocket = new DatagramSocket(8089);
			byte[] receiveData = new byte[1024];
			byte[] sendData = new byte[1024];
			
			while(true) {
				DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
				serverSocket.receive(receivePacket);
				String packetReceived = new String (receivePacket.getData());
				System.out.println("RECEIVED:" + packetReceived);
				 InetAddress receiverHost = receivePacket.getAddress();
				 int receiverPort = receivePacket.getPort();
				 String packetSend = packetReceived.toUpperCase();
				 sendData = packetSend.getBytes();
				 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receiverHost, receiverPort);
				 serverSocket.send(sendPacket);
				 serverSocket.close();
			}
			
			*/
			
	      //SPSUSocket openSocket = new SPSUSocket(8089); 
	  
	     
	     
	  
	     /* try 
	        { 
	  
	    	  DatagramPacket datagram = openSocket.receiveMessage();
	  		  InetAddress ipAddress = datagram.getAddress(); 
	          int port = datagram.getPort(); 
	          byte [] request = new byte [datagram.getLength()];
	          request = datagram.getData();
	         
	          
	          InputStream dataReader = new FileInputStream(request.toString());
	          byte [] response = new byte[1024];
	          int file = dataReader.read(response);
	          
	          if (file == 0 || file == -1){
	        	  System.out.println("SERVER SAYS: File does not exist!");
	        	  dataReader.close();
	          }
	          else{
	              dataReader.close();
	              openSocket.sendMessage(ipAddress, port, response);
	              openSocket.close();
	          }
	        }
	      
	      catch(Exception ex){
	    	  ex.printStackTrace();
	      }*/

     }
}
